﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Data.SQLite;

namespace MusicApp
{
    public partial class FormMain : Form
    {
        string curPath;
        SetStr curSetStr = new SetStr();
        public FormMain()
        {
            InitializeComponent();
            var moudle = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0];
            curPath = System.IO.Path.GetDirectoryName(moudle.FullyQualifiedName);
            curSetStr = ConData();
            musics = curSetStr.musicfilenames;
            //在控件中显示当前的播放方式
            switch (curSetStr.bffs)
            {
                case "dqxh":
                    UiSymBtnXHBF.Selected = false;
                    UiSymBtnSJBF.Selected = false;
                    UiSysBtnDQBF.Selected = true;
                    break;
                case "sjbf":
                    UiSymBtnXHBF.Selected = false;
                    UiSymBtnSJBF.Selected = true;
                    UiSysBtnDQBF.Selected = false;
                    break;
                default:
                    UiSymBtnXHBF.Selected = true;
                    UiSymBtnSJBF.Selected = false;
                    UiSysBtnDQBF.Selected = false;
                    break;
            }

            if (ListBoxMusic.Items.Count != 0)
            {
                ListBoxMusic.SelectedIndex = 0;
            }

            timer1.Start();
        }

        private SetStr ConData()
        {
            SetStr setStr = new SetStr();
            List<string> filenames = new List<string>();
            List<string> names = new List<string>();
            string dbPath = curPath + @"\Settings.db";
            SQLiteConnection conn = new SQLiteConnection("data source=" + dbPath);
            conn.Open();
            SQLiteCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from 'bofangfangshi'";
            SQLiteDataReader sr = cmd.ExecuteReader();
            if (sr.Read())
            {
                setStr.bffs = sr.GetString(0);
            }
            //datareader 必须要先关闭，否则 commandText 不能赋值
            sr.Close();

            cmd.CommandText = "select * from 'musicfilenames'";
            sr = cmd.ExecuteReader();
            while (sr.Read())
            {
                filenames.Add(sr.GetString(0));
            }
            sr.Close();

            cmd.CommandText = "select * from 'musicnames'";
            sr = cmd.ExecuteReader();
            while (sr.Read())
            {
                names.Add(sr.GetString(0));
                ListBoxMusic.Items.Add(sr.GetString(0));
            }
            conn.Close();
            setStr.musicfilenames = filenames;
            setStr.musicnames = names;
            return setStr;
        }
        internal struct SetStr
        {
            //播放方式——sxbf,sjbf,dqxh
            public string bffs;
            //musicfilenames
            public List<string> musicfilenames;
            //musicnames
            public List<string> musicnames;
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            //在程序加载的时候加载一个皮肤
            skinEngine1.SkinFile = curPath + @"\皮肤\Wave.ssk";
            //取消自动播放功能
            Player.settings.autoStart = false;
        }

        private void UiSymBtnStart_Click(object sender, EventArgs e)
        {
            UiSymBtnStart.Selected = true;
            UiSymBtnTZ.Selected = false;
            UiSymBtnZT.Selected = false;
            Player.URL = musics[ListBoxMusic.SelectedIndex];
            Player.Ctlcontrols.play();
        }

        private void UiSymBtnZT_Click(object sender, EventArgs e)
        {
            UiSymBtnStart.Selected = false;
            UiSymBtnTZ.Selected = false;
            UiSymBtnZT.Selected = true;
            Player.Ctlcontrols.pause();
        }

        private void UiSymBtnTZ_Click(object sender, EventArgs e)
        {
            UiSymBtnStart.Selected = false;
            UiSymBtnTZ.Selected = true;
            UiSymBtnZT.Selected = false;
            Player.Ctlcontrols.stop();
        }
        List<string> musics = new List<string>();
        private void UiSymBtnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //设置对话框属性
            ofd.Title = "选择音乐文件";
            //多选
            ofd.Multiselect = true;
            //文件类型
            ofd.Filter = "音乐文件|*.mp3|所有文件|*.*";
            //初始路径
            ofd.InitialDirectory = curPath + @"\music";
            //展示对话框
            ofd.ShowDialog();
            //获取文件全路径
            string[] filePaths = ofd.FileNames;
            
            for (int i = 0; i < filePaths.Length; i++)
            {
                musics.Add(filePaths[i]);
                ListBoxMusic.Items.Add(Path.GetFileName(filePaths[i]));
            }
        }

        private void ListBoxMusic_DoubleClick(object sender, EventArgs e)
        {
            if (ListBoxMusic.SelectedIndex == -1) return;

            UiSymBtnStart.Selected = true;
            UiSymBtnTZ.Selected = false;
            UiSymBtnZT.Selected = false;
            Player.URL = musics[ListBoxMusic.SelectedIndex];
            Player.Ctlcontrols.play();
        }

        private void UiSymBtnSYX_Click(object sender, EventArgs e)
        {
            int index = ListBoxMusic.SelectedIndex;
            if (index == -1) return;
            index--;
            if (index < 0) index = ListBoxMusic.Items.Count - 1;

            ListBoxMusic.SelectedIndex = index;

            Player.URL = musics[index];

            if (UiSymBtnStart.Selected)
            {
                Player.Ctlcontrols.play();
            }
        }

        private void UiSymBtnXYS_Click(object sender, EventArgs e)
        {
            int index = ListBoxMusic.SelectedIndex;
            if (index == -1) return;
            index++;
            if (index == ListBoxMusic.Items.Count) index = 0;

            ListBoxMusic.SelectedIndex = index;

            Player.URL = musics[index];

            if (UiSymBtnStart.Selected)
            {
                Player.Ctlcontrols.play();
            }
        }
        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            //读取我们的图片文件
            string[] imgsPath = Directory.GetFiles(curPath + @"\美女");
            i++;
            if (i == imgsPath.Length)
            {
                i = 0;
            }
            PictureBoxMN.Image = Image.FromFile(imgsPath[i]);
        }

        private void UiSymBtnXHBF_Click(object sender, EventArgs e)
        {
            UiSymBtnXHBF.Selected = true;
            UiSymBtnSJBF.Selected = false;
            UiSysBtnDQBF.Selected = false;
            curSetStr.bffs = "xhbf";
        }

        private void UiSymBtnSJBF_Click(object sender, EventArgs e)
        {
            UiSymBtnXHBF.Selected = false;
            UiSymBtnSJBF.Selected = true;
            UiSysBtnDQBF.Selected = false;
            curSetStr.bffs = "sjbf";
        }

        private void UiSysBtnDQBF_Click(object sender, EventArgs e)
        {
            UiSymBtnXHBF.Selected = false;
            UiSymBtnSJBF.Selected = false;
            UiSysBtnDQBF.Selected = true;
            curSetStr.bffs = "dqxh";
        }

        private void UiSymBtnSC_Click(object sender, EventArgs e)
        {
            int count = ListBoxMusic.SelectedItems.Count;
            for (int i = 0; i < count; i++)
            {
                //先删集合
                musics.RemoveAt(ListBoxMusic.SelectedIndex);
                //再删列表
                ListBoxMusic.Items.RemoveAt(ListBoxMusic.SelectedIndex);
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (Player.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                MusicInfo.Text = Player.Ctlcontrols.currentPositionString + "\r\n" + Player.currentMedia.durationString;
                if (Player.currentMedia.duration - Player.Ctlcontrols.currentPosition <= 1)
                {
                    switch (curSetStr.bffs)
                    {
                        case "dqxh":
                            Player.Ctlcontrols.currentPosition = 0;
                            break;
                        case "sjbf":
                            Random ran = new Random();
                            int indexsj = ran.Next(ListBoxMusic.Items.Count);
                            ListBoxMusic.SelectedIndex = indexsj;
                            Player.URL = musics[indexsj];
                            if (UiSymBtnStart.Selected)
                            {
                                Player.Ctlcontrols.play();
                            }
                            break;
                        default:
                            int index = ListBoxMusic.SelectedIndex;
                            if (index == -1) return;
                            index++;
                            if (index == ListBoxMusic.Items.Count) index = 0;

                            ListBoxMusic.SelectedIndex = index;

                            Player.URL = musics[index];

                            if (UiSymBtnStart.Selected)
                            {
                                Player.Ctlcontrols.play();
                            }
                            break;
                    }
                }
                
            }
        }
    }
}
